package com.yong.ssj.util;

import java.io.File;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/util")
public class UploadController {
	//身份证
	//fileId 与input的name相同
	@RequestMapping(value = "/uploadIdcard", method = RequestMethod.POST)
	@ResponseBody
	public String uploadIdcard(MultipartFile Idcard,HttpServletRequest request) {  
		  
        System.out.println("开始");  
        String path = request.getSession().getServletContext().getRealPath("upload");  
        String fileName = Idcard.getOriginalFilename();
        fileName = UUID.randomUUID()+fileName;//生成唯一的名的文件
        System.out.println(path);
        String dateDir = DateTimeUtils.formatDate(new Date(), "yyyy-MM-dd");
        File targetFile = new File(path+"/Idcard/"+dateDir, fileName);  
        if(!targetFile.exists()){  
            targetFile.mkdirs();//创建目录  
        }  
        //保存  
        try {  
        	Idcard.transferTo(targetFile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        //返回相对web项目路径，注意不是绝对路径
        return request.getContextPath()+"/upload/Idcard/"+dateDir+"/"+fileName;  
    }  
	
	//驾驶证
	@RequestMapping(value = "/uploadDriving", method = RequestMethod.POST)
	@ResponseBody
	public String uploadDriving(MultipartFile driving,HttpServletRequest request) {  
		  
        System.out.println("开始");  
        String path = request.getSession().getServletContext().getRealPath("upload");  
        String fileName = driving.getOriginalFilename();
        fileName = UUID.randomUUID()+fileName;//生成唯一的名的文件
        System.out.println(path);
        String dateDir = DateTimeUtils.formatDate(new Date(), "yyyy-MM-dd");
        File targetFile = new File(path+"/driving/"+dateDir, fileName);  
        if(!targetFile.exists()){  
            targetFile.mkdirs();//创建目录  
        }  
        //保存  
        try {  
        	driving.transferTo(targetFile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        //返回相对web项目路径，注意不是绝对路径
        return request.getContextPath()+"/upload/driving/"+dateDir+"/"+fileName;  
    }  
	
	//凭证
	@RequestMapping(value = "/uploadPz", method = RequestMethod.POST)
	@ResponseBody
	public String uploadPz(MultipartFile pz,HttpServletRequest request) {  
		  
        System.out.println("开始");  
        String path = request.getSession().getServletContext().getRealPath("upload");  
        String fileName = pz.getOriginalFilename();
        fileName = UUID.randomUUID()+fileName;//生成唯一的名的文件
        System.out.println(path);
        String dateDir = DateTimeUtils.formatDate(new Date(), "yyyy-MM-dd");
        File targetFile = new File(path+"/pz/"+dateDir, fileName);  
        if(!targetFile.exists()){  
            targetFile.mkdirs();//创建目录  
        }  
        //保存  
        try {  
        	pz.transferTo(targetFile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        //返回相对web项目路径，注意不是绝对路径
        return request.getContextPath()+"/upload/pz/"+dateDir+"/"+fileName;  
    }  
	
	//银行卡
	@RequestMapping(value = "/uploadBank", method = RequestMethod.POST)
	@ResponseBody
	public String uploadBank(MultipartFile Bank,HttpServletRequest request) {  
		  
        System.out.println("开始");  
        String path = request.getSession().getServletContext().getRealPath("upload");  
        String fileName = Bank.getOriginalFilename();
        fileName = UUID.randomUUID()+fileName;//生成唯一的名的文件
        System.out.println(path);
        String dateDir = DateTimeUtils.formatDate(new Date(), "yyyy-MM-dd");
        File targetFile = new File(path+"/Bank/"+dateDir, fileName);  
        if(!targetFile.exists()){  
            targetFile.mkdirs();//创建目录  
        }  
        //保存  
        try {  
        	Bank.transferTo(targetFile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        //返回相对web项目路径，注意不是绝对路径
        return request.getContextPath()+"/upload/Bank/"+dateDir+"/"+fileName;  
    }  
	
	//通讯录
	@RequestMapping(value = "/uploadPhone", method = RequestMethod.POST)
	@ResponseBody
	public String uploadPhone(MultipartFile Phone,HttpServletRequest request) {  
		  
        System.out.println("开始");  
        String path = request.getSession().getServletContext().getRealPath("upload");  
        String fileName = Phone.getOriginalFilename();
        fileName = UUID.randomUUID()+fileName;//生成唯一的名的文件
        System.out.println(path);
        String dateDir = DateTimeUtils.formatDate(new Date(), "yyyy-MM-dd");
        File targetFile = new File(path+"/Phone/"+dateDir, fileName);  
        if(!targetFile.exists()){  
            targetFile.mkdirs();//创建目录  
        }  
        //保存  
        try {  
        	Phone.transferTo(targetFile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        //返回相对web项目路径，注意不是绝对路径
        return request.getContextPath()+"/upload/Phone/"+dateDir+"/"+fileName;  
    }  
	
}
